layui.use(['form', 'layer', 'dropdown'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        dropdown = layui.dropdown;

    //左侧菜单点击事件，其中 docDemoMenu1 对应的是菜单结构上的 id 指
    dropdown.on('click(docDemoMenu1)', function (options) {
        var othis = $(this); //当前菜单列表的 DOM 对象
        console.log(options); //菜单列表的 lay-options 中的参数
        switch (options.id) {
            case 1:
                window.location.href = "tool01.html";
                break;
            case 2:
                window.location.href = "tool02.html";
                break;
            case 3:
                window.location.href = "tool03.html";
                break;
            case 4:
                window.location.href = "tool04.html";
                break;
            case 5:
                window.location.href = "tool05.html";
                break;
        }
    });

    //监听身份证输入
    $(".sid").change(function () {
        $.ajax({
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            url: serverApi3 + "/umsmember/findumsMemberVOBySId?sid=" + $(".sid").val(),
            success: function (result) {
                if (result.code === 200) {
                    $(".name").val(result.data.name)
                    $(".age").val(result.data.age)
                    $(".phone").val(result.data.phone)
                    if (result.data.gender === 1) {
                        $(".nan").attr("checked", true);
                    } else if (result.data.gender === 2) {
                        $(".nv").attr("checked", true);
                    }
                    form.render();
                }
            }
        });
    });

    form.verify({
        username: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                return '患者姓名不能有特殊字符';
            }
            if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                return '患者姓名首尾不能出现下划线\'_\'';
            }
            if (/^\d+\d+\d$/.test(value)) {
                return '患者姓名不能全为数字';
            }
        }

        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        , pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ]
    });

    form.on("submit(submit)", function (data) {
        console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
        //清楚原医院记录
        sessionStorage.removeItem('pharmacyVO')

        $.ajax({
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            url: serverApi3 + "/umsmember/findumsMemberVOBySId?sid=" + data.field.sid,
            success: function (result) {
                if (result.code === 200) {
                    // layer.msg("success")
                    let memberData3 = {
                        "memberId": result.data.id,
                        // "orderId": data.id,
                        // "diagnosisId": data.diagnosisId,
                        "headImg": result.data.icon,
                        "name": result.data.name,
                        "phone": result.data.phone,
                        "address": result.data.jwd,
                        "sid": result.data.sid,
                    };
                    sessionStorage.setItem('memberData3', JSON.stringify(memberData3));

                    let index = layui.layer.open({
                        title: "快速开药",
                        type: 2,
                        maxmin: true, //开启最大化最小化按钮
                        area: ["590px", "660px"],
                        content: "tool01Add.html",
                        shadeClose: true,
                        success: function (layero, index) {

                        }
                    });
                } else {
                    layer.msg("此用户不存在！");
                }
            },
            error: function (e) {
                layer.msg("信息填写有误！");
            }
        });

        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});
